<?php if (!defined('CORE_DIR')) die('Wrong entry point!');

/* 
 * Copyright 2010 Anton Muraviev a.k.a kuromaisu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

Aero::import('libs/errors/handler');
Aero::define('AeroDisplayErrors', AS_SINGLETON);

/* -----------------------------------------------------------------------------
 * Class: AeroDisplayErrors
 * -----------------------------------------------------------------------------
 *
 */
class AeroDisplayErrors implements AeroErrorHandlerInterface
{
	protected $is_localhost;
	
	public function __construct()
	{
		$this->is_localhost = isset($_SERVER['SERVER_ADDR']) && isset($_SERVER['REMOTE_ADDR']) 
			&& $_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR'];
		
		// Prevent early output
		ob_start();
		
		AeroErrorHandler::addPlugin($this);
	}
	
	public function priority()
	{
		return ERROR_HANLDER_PLUGIN_PRIORITY_LOW;
	}
	
	public function onShutdown()
	{
		# empty
	}
	
	public function onNotice($error)
	{
		_aero_display_inline_error($error);
	}
	
	public function onCritical($error)
	{
		// FIXME: Capturing critical errors does not work...
		_aero_display_inline_error($error);
	}
	
	public function onError($error, $backtrace = array())
	{
		if (!Aero::option('errors.display', $this->is_localhost))
		{
			Aero::load('libs/response')->error(500);
		}
		
		// Clean output buffer
		while (ob_get_level() !== 0) {
			ob_end_clean();
		}
		
		_aero_display_critical_error($error,$backtrace,AeroErrorHandler::notices(), $this->is_localhost);
	}
}

// =======================
// = Inline error output =
// =======================
function _aero_display_inline_error($error)
{
?>
	<span style="display:inline-block; margin:5px; padding:3px; font-size: 16px; line-height: 18px; background:red url('')">
		<span style="display:inline-block; padding:10px; color:gray; background:black url('') 4px 45% no-repeat; padding-left:20px;">
			<span style="color:white;font-weight:bold; font-family: Arial, sans-serif"><?= $error['message'] ?></span>
			in <span style="background:#111;color:white;font-weight: normal; font-family: monospace"><?= $error['file'] ?></span>
			on line <span style="background:#111;color:white;font-weight: normal; font-family: monospace"><?= $error['line'] ?></span>
		</span>
	</span>
<?php
}

// =========================
// = Critical error output =
// =========================
function _aero_display_critical_error($error, $backtrace, $error_log, $is_localhost)
{
	$Response = Aero::load('libs/response');
	$Response->render('libs/errors/display/template', array(
		'error' => $error,
		'backtrace' => $backtrace,
		'error_log' => $error_log,
		'is_localhost' => $is_localhost
	));
	$Response->display();
}